<?php
namespace utils;

use utils\FileTool;

class CryptoTool
{
	var $key = "2C4DD1CC9KAX4TA9";
	var $iv = "2C4DD1CC9KAX4TA9";
	
	function decrypt($txt)
	{
	    //16字节对齐
	    $str_padded = $txt;
	    if (strlen($str_padded) % 16) {
	        $str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0");
	    }
		$decrypted = openssl_decrypt($str_padded, 'AES-128-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
		$decrypted = str_replace("\0", "", $decrypted);
		return 	$decrypted;
	}

	function encrypt($txt)
	{
		//16字节对齐
		$str_padded = $txt;
		if (strlen($str_padded) % 16) {
			$str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0");
		}
	  
	    //加密
		$ret = openssl_encrypt($str_padded,"AES-128-CBC",$this->key,OPENSSL_ZERO_PADDING,$this->iv);
	
	    return $ret;
	}
	
	function decode($pathSvr,$lenOri)
	{
	    //解密
	    $ft = new FileTool();
	    $data = $ft->readBytes($pathSvr);
	    //PHP7已废弃此模块
	    //$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $data, MCRYPT_MODE_CBC, $this->iv);
	    $decrypted = openssl_decrypt($data, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $this->iv);
	    $decrypted = substr($decrypted, 0, $lenOri);
	    return 	$decrypted;
	}
	
	/**
	 * aligo 加密算法：aes,sm4
	 */
	function decrypt2($aligo,$block)
	{
	    if(StringTool::iequals($aligo,"sm4")) return $this->sm4_decode($block);
	    else if (StringTool::iequals($aligo,"aes/ecb")) return $this->aes_ecb_decode($block);
	    return $this->aes_decode($block);
	}
	
	//aes/cbc/pkcs
	function aes_encode($block)
	{
		return openssl_encrypt($block, "aes-128-cbc", $this->key, 0, $this->key);
	}
	
	//aes/cbc/pkcs
	function aes_decode($block)
	{
		return openssl_decrypt(base64_encode($block), "aes-128-cbc", $this->key, 0, $this->key);
	}
	
	function aes_ecb_decode($block)
	{
	    
	}
	
	function sm4_encode($s)
	{
		return openssl_encrypt($s, "sm4-cbc", $this->key, 0, $this->key);
	}
	
	function sm4_decode($s)
	{
		return openssl_decrypt(base64_encode($s), "sm4-cbc", $this->key, 0, $this->key);
	}
	
	/**
	 * 解密数据
	 * @param $data 二进制数据
	 * @param $len 数据长度
	 * @return string
	 */
	function decodeData($data,$len)
	{
	    $decrypted = openssl_decrypt($data, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $this->iv);
	    $decrypted = substr($decrypted, 0, $len);
	    return 	$decrypted;
	}
	
	function token($f,$action)
	{
		$str = $f->id . $f->nameLoc . $action;
		if($action == "block") $str = $f->id . $f->pathSvr . $action;
		$str = md5($str);
		$str = $this->encrypt($str);
		return $str;
	}
	
	public static function hmac_sha256_encode($data,$key)
	{
		if ($data== null || empty($data)) 
		{
			return $data;
		}
		return base64_encode(hash_hmac("sha1", $data, $key));
	}

	public function hmac_sha256_decode($data)
	{
		if ($data== null || empty($data))
		{
			return $data;
		}
		$secret_key = "your key";
		$iv = "your iv";
		$result= openssl_decrypt(base64_decode($data), "aes-256-cbc", $secret_key, OPENSSL_RAW_DATA, $iv);
		return $result;
	}
}
?>